﻿using BPCSharp;
using Microsoft.PointOfService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BAR_POS
{
    static class CreditCardPayment
    {
        public static string ReceiptNo;
        public static double RecieptFoodAmount = 0;
        public static double RecieptTaxAmount = 0;
        public static double RecieptTipAmount = 0;
        public static int TransID = 0;
        public static double PreAuthAmount = 0;
        public static double CaptureAmount = 0;
        public static double AmountToProcess = 0;

        static string accountID = "100212803318";
        static string secretKey = "OCOMHSPDHXEYFPU3VYTYPXQVRBK5OQLB";
        static string mode = "TEST";
        static string tranID;

        public static string CardMethod;
        public static string CreationDate;
        public static string CardType;
        public static string MaskedCardNumber;
        public static string AuthCode;
        public static string transID;


        public static void ProcessAuth2()
        {
            try
            {
                StaticProperties.CreditCardReader.Open();
                StaticProperties.CreditCardReader.Claim(2500);
                StaticProperties.CreditCardReader.DataEvent += new DataEventHandler(msrDevice_DataEvent);
                StaticProperties.CreditCardReader.DataEventEnabled = true;
                StaticProperties.CreditCardReader.DeviceEnabled = true;


            }
            catch (Exception E)
            {
                MessageBox.Show(E.Message + "    " + E.InnerException);
            }

            //msrDevice_DataEvent();
        }

        private static void msrDevice_DataEvent(object sender, DataEventArgs e)
        {
            //OpenTickets._main.progressPanel1.Caption = "PROCESSING CARD";

            string ActNo =  StaticProperties.CreditCardReader.AccountNumber;
            string CustName =  StaticProperties.CreditCardReader.Surname + "/" + StaticProperties.CreditCardReader.FirstName;
            string ExpDate = StaticProperties.CreditCardReader.ExpirationDate;

            string Data = "%B" + ActNo + "^" + CustName + " ^" + ExpDate.PadRight(31, '0') + "?;" + ActNo + "=" + ExpDate.PadRight(15, '0') + "?";// "%B" + ActNo + "^" + CustName + " ^" + ExpDate.PadRight(31, '0') + "?;" + ActNo + "=" + ExpDate.PadRight(15, '0') + "?";

            try
            {
                BluePayPayment payment = new BluePayPayment(accountID, secretKey, mode);


                payment.swipe(Data);
                payment.setAmountFood(RecieptFoodAmount.ToString());
                payment.setAmountTax(RecieptTaxAmount.ToString());

                payment.auth(PreAuthAmount.ToString());

                payment.Process();
                string status = payment.getStatus().ToUpper();

                //MessageBox.Show(status);

                if (status == "APPROVED")
                {
                    //OpenTickets._main.progressPanel1.Caption = payment.getMessage();
                    //OpenTickets._main.progressPanel1.Description = "PRINTING CUSTOMER RECEIPTS";
                    //OpenTickets._main.progressPanel1.ShowDescription = true;

                    CardMethod = "Swiped";
                    AuthCode = payment.getAuthCode();
                    CreationDate = payment.getCreationDate();
                    CardType = payment.getCardType();
                    MaskedCardNumber = payment.getMaskedPaymentAccount();
                    transID = payment.getTransID();
                    

                    //TransID = Convert.ToInt32(payment.getTransID());

                    try
                    {
                        DataRow newCustomersRow = DataTableClass.CCPaymentInfo.NewRow();//dataSet1.Tables["Customers"].NewRow();

                        //newCustomersRow["EntryNo"] = ReceiptNo;
                        newCustomersRow["ReceiptNo"] = ReceiptNo;
                        newCustomersRow["ReceiptFoodAmount"] = RecieptFoodAmount;
                        newCustomersRow["ReceiptTaxAmount"] = RecieptTaxAmount;
                        newCustomersRow["CCPaymentTransID"] = payment.getTransID();
                        newCustomersRow["CCPreAuthAmount"] = PreAuthAmount;


                        DataTableClass.CCPaymentInfo.Rows.Add(newCustomersRow);
                    }
                    catch (Exception E)
                    {
                        MessageBox.Show(E.ToString());
                    }

                    PrintingClass.PrintCreditCardSlipPage();

                }
                else if (status == "DECLINED")
                {
                    //OpenTickets._main.btnCreditCard.Enabled = true;
                    //OpenTickets._main.btnCash.Enabled = true;

                }
                else if (status == "ERROR")
                {
                    MessageBox.Show(payment.getMessage());
                    //OpenTickets._main.btnCreditCard.Enabled = true;
                    //OpenTickets._main.btnCash.Enabled = true;
                }
                else if (status == "MISSING")
                {
                    //OpenTickets._main.btnCreditCard.Enabled = true;
                    //OpenTickets._main.btnCash.Enabled = true;
                }
            }
            catch (Exception E) { MessageBox.Show(E.ToString()); }
            finally { CloseMSR(); }

        }

        private static void CloseMSR()
        {

            if (StaticProperties.CreditCardReader != null)
            {
                StaticProperties.CreditCardReader.Release();
                StaticProperties.CreditCardReader.Close();
            }
        }

        public static void ProcessCapture(string Amount, string TransactionID)
        {
            //OpenTickets._main.progressPanel1.Caption = "Processing Payment";
            //OpenTickets._main.progressPanel1.Description = "PRINTING CUSTOMER RECEIPTS";
            //OpenTickets._main.progressPanel1.ShowDescription = false;

            BluePayPayment payment = new BluePayPayment(accountID, secretKey, mode);

            payment.capture(tranID, Amount);
            payment.Process();

            
        }
    }
}
